package com.dcrzl.shcz.module.sys.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import com.dcrzl.shcz.common.dto.LoginDto;
import com.dcrzl.shcz.common.dto.UpdatePasswordDto;
import com.dcrzl.shcz.common.entity.sys.MiniUser;
import com.dcrzl.shcz.common.entity.sys.SysUser;
import com.dcrzl.shcz.common.utils.*;
import com.dcrzl.shcz.module.sys.service.MiniUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 小程序用户
 *
 * @author dcrzl
 * @email 1728795786@qq.com
 * @date 2025-01-06 10:10:35
 */
@RestController
@RequestMapping("/mini/user")
public class MiniUserController extends BaseController {
    @Autowired
    private MiniUserService miniUserService;

    /**
     * 列表
     */
    @GetMapping("/list")
    public Result<List<MiniUser>> list(MiniUser miniUser) {
        return miniUserService.selectList(miniUser);
    }

    /**
     * 列表分页
     */
    @GetMapping("/page")
    public PageInfo<MiniUser> page(MiniUser miniUser) {
        return miniUserService.selectUPage(miniUser);
    }

    /**
     * 信息
     */
    @GetMapping("/info")
    public Result<MiniUser> info(MiniUser miniUser) {
        miniUser.setId(this.getUserId());
        return miniUserService.selectOne(miniUser);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public Result<MiniUser> info(@PathVariable("id") Long id) {
        return miniUserService.selectUOne(new MiniUser().setId(id));
    }


    /**
     * 保存
     */
    @PostMapping("/save")
    public Result<Integer> save(@RequestBody MiniUser miniUser) {
        miniUser.setPassword(EncryptionUtil.getEncryption("md5", "123456"));
        return miniUserService.uSave(miniUser);
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result<Integer> update(@RequestBody MiniUser miniUser) {
        return miniUserService.uUpdateById(miniUser);
    }

    /**
     * 修改
     */
    @PutMapping("/update/password")
    public Result<Integer> updatePassword(@RequestBody UpdatePasswordDto updatePasswordDto) {
        updatePasswordDto.setUserId(this.getUserId());
        if(!Objects.equals(updatePasswordDto.getRePassword(),updatePasswordDto.getPassword())){
            return new Result<Integer>().setSuccess(false).setMsg("两次密码不一致").setCode(SysConstant.PASSWORD_ERROR_CODE_RES);
        }
        return miniUserService.updatePassword(updatePasswordDto);
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public Result<Integer> delete(@RequestBody Long[] ids) {
        return miniUserService.deleteBatchIds(Arrays.asList(ids));
    }

    @PostMapping("/init/password")
    public Result<Integer> initPassword(@RequestBody MiniUser miniUser) {
        return miniUserService.initPassword(miniUser);
    }

    /**
     * 用户登录
     *
     * @return
     */
    @PostMapping("/login")
    public Result<Object> login(@RequestBody MiniUser miniUser) {
        miniUser.setPassword(EncryptionUtil.getEncryption("md5", miniUser.getPassword()));
        return miniUserService.login(miniUser);
    }

}
